package com.aisino.cvasp.application.service;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import java.io.IOException;
import java.util.List;

public class CreateBarcode {




    public static void CreateBarcode( AcroFields acroFields, PdfStamper ps) throws DocumentException, IOException {

        List<AcroFields.FieldPosition> positions =  acroFields.getFieldPositions("barCode1");
        Rectangle signRect = positions.get(0).position;
        int pageNo = positions.get(0).page;//该表单所在页码

        float x = signRect.getLeft(); //该表单坐标
        float y = signRect.getBottom();//该表单坐标
        float widthy = signRect.getWidth();//该表单宽
        float height = signRect.getHeight();//该表单高


        Barcode128 barcode128 = new Barcode128();

        //条形码字号
        barcode128.setSize(10);

        barcode128.setBarHeight(50);

        //条形码与数字间距
        barcode128.setBaseline(10);
//            //条形码值
        barcode128.setCode("URTY56757");
        barcode128.setCodeType(Barcode.CODE128);
        barcode128.setAltText("URTY56757");

        barcode128.setStartStopText(false);
        barcode128.setExtended(true);
        PdfContentByte cb = ps.getOverContent(pageNo);
        Image image = barcode128.createImageWithBarcode(cb, null, null);
        image.scaleAbsolute(widthy, height); //设置图片宽高
        image.setAbsolutePosition(x, y); //设置图片坐标

        cb.addImage(image);

    }



}
